package com.dynamic.mybatis.core.metadata;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

public class DynamicPage implements Serializable {
    private long total;//总数
    private Integer pageSize;//每页显示条数，默认 10
    private Integer pageNumber;//当前页
    private Integer offset;
    private Integer limit;
    private long pages = 0;//总页数
    private List<?> records;// 每行显示的内容


    public final static String PAGE_NUMBER = "pageNumber";
    public final static String PAGE_SIZE = "pageSize";
    public final static String TOTAL = "total";
    public final static String RECORDS = "records";


    public final static String OFFSET = "offset";
    public final static String LIMIT = "limit";


    public DynamicPage() {
        this.pageNumber = 1;
        this.pageSize = 10;
    }

    public DynamicPage(Map<String, Object> parameter) {
        if (parameter != null) {
            if (parameter.get(PAGE_NUMBER) != null) {
                this.pageNumber = Integer.valueOf(parameter.get(PAGE_NUMBER).toString());
            }
            if (parameter.get(PAGE_SIZE) != null) {
                this.pageSize = Integer.valueOf(parameter.get(PAGE_SIZE).toString());
            }
            if (this.pageNumber == null) {
                if (parameter.get(OFFSET) != null) {
                    this.offset = Integer.valueOf(parameter.get(OFFSET).toString());
                }
                if (parameter.get(LIMIT) != null) {
                    this.limit = Integer.valueOf(parameter.get(LIMIT).toString());
                }
            }
        }

        if (this.offset != null && this.offset >= 0) {
            this.offset = this.offset != null && this.offset >= 0 ? this.offset : 0;
            this.limit = this.limit != null && this.limit >= 1 ? this.limit : 10;
        } else {
            this.pageNumber = this.pageNumber != null && this.pageNumber >= 1 ? this.pageNumber : 1;
            this.pageSize = this.pageSize != null && this.pageSize >= 1 ? this.pageSize : 10;
        }
    }

    public DynamicPage result(List<?> records, long total) {
        this.records = records;
        this.total = total >= 0 ? total : 0;
        return this;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public void setPageNumber(Integer pageNumber) {
        this.pageNumber = pageNumber;
    }

    public void setOffset(Integer offset) {
        this.offset = offset;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    /**
     * 总页数
     */
    public long getPages() {
        long pages = 0;
        int pageSize = 0;
        if (this.pageSize != null) {
            pageSize = this.pageSize;
        } else if (this.limit != null) {
            pageSize = this.limit;
        }
        if (pageSize == 0) {
            return pages;
        }
        pages = this.total / pageSize;
        if (this.total % pageSize != 0) {
            pages++;
        }
        return this.pages = pages;
    }

    public Long getTotal() {
        return total;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public Integer getPageNumber() {
        return pageNumber;
    }

    public List<?> getRecords() {
        return records;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public void setPages(int pages) {
        this.pages = pages;
    }

    public void setRecords(List<?> records) {
        this.records = records;
    }

    public Integer getOffset() {
        if (offset == null && pageNumber != null && pageSize != null) {
            return (pageNumber - 1) * pageSize;
        }
        return offset;
    }

    public Integer getLimit() {
        if (limit == null && pageSize != null) {
            return pageSize;
        }
        return limit;
    }
}
